<?php

namespace app\services\exam;

use app\dao\exam\ExamSendDao;
use app\exceptions\AuthException;
use app\services\BaseService;

class ExamSendService extends BaseService
{

    public function __construct(ExamSendDao $dao)
    {
        $this->dao = $dao;
    }

    public function index(array $where)
    {
        return $this ->dao -> index($where);
    }

    public function createOrUpdate(array $where)
    {
        if ($where['id']) return $this ->dao ->updateDao($where);
        return $this -> dao -> create($where);
    }

    public function delete(array $where)
    {
        return $this -> dao -> deleteDao($where);
    }

    /**
     * 获取推流地址
     * 如果不传key和过期时间，将返回不含防盗链的url
     * @param domain 您用来推流的域名
     *        streamName 您用来区别不同推流地址的唯一流名称
     *        key 安全密钥
     *        time 过期时间 sample 2016-11-12 12:00:00
     * @return String url
     */
    function getPushUrl($domain, $streamName, $key = null, $time = null){
        if($key && $time){
            $txTime = strtoupper(base_convert(strtotime($time),10,16));
            //txSecret = MD5( KEY + streamName + txTime )
            $txSecret = md5($key.$streamName.$txTime);
            $ext_str = "?".http_build_query(array(
                    "txSecret"=> $txSecret,
                    "txTime"=> $txTime
                ));
        }
        return "webrtc://".$domain."/live/".$streamName . (isset($ext_str) ? $ext_str : "");
    }


    //-----------------------------------------api-----------------------------------------------------------
    /*
     * 获取综合测评测试目录
     */
    public function getExam(array $where){

        $exam = $this->dao->getExam($where);
        for ($i=0;$i<count($exam);$i++){
            $where['exam_id'] = $exam[$i]['id'];
            $data = $this->dao->getUserExam($where);
            $exam[$i]['exam_num']=$exam[$i]['exam_num']-count($data);
        }
        return $exam;


    }
}